<?php
/*
Plugin Name: PS_BP_TOKSTA,  toksta* im/chat plugin for BuddyPress
Plugin URI: http://www.soderlind.no/archives/2009/02/27/toksta-chat-plugin-for-buddypress/
Description: Enables toksta* Chat on your Blog
Version: 0.93
Author: Per Soderlind aka PerS
Author URI: http://www.soderlind.no
*/

require_once( 'bp-core.php');

function ps_bp_toksta_load_l10n() {
	load_plugin_textdomain('ps_bp_toksta',  LANGDIR);
}
add_action('init','ps_bp_toksta_load_l10n');

function ps_bp_toksta_add_admin_subpanel(){
	get_currentuserinfo();
	if (!is_site_admin()) return false;	
	add_submenu_page('wpmu-admin.php',__('toksta* configuration','ps_bp_toksta'),__('toksta* configuration','ps_bp_toksta'), 'administrator', basename(__FILE__), 'ps_bp_toksta_adminpage');
}

function ps_bp_toksta_adminpage()
{
	if ( isset($_POST['action']) && ( $_POST['action'] == 'update_ps_bp_toksta' )){
	
		update_site_option('ps_bp_toksta_app_id', $_POST['ps_bp_toksta_app_id']);
		update_site_option('ps_bp_toksta_app_password', $_POST['ps_bp_toksta_app_password']);
		update_site_option('ps_bp_toksta_show_siteadmin', $_POST['ps_bp_toksta_show_siteadmin']);
		update_site_option('ps_bp_toksta_max_members', $_POST['ps_bp_toksta_max_members']);
		?><div id="message" class="updated fade"><p><?php _e('Options saved.','ps_bp_toksta') ?></p></div><?php
	}
	$ps_bp_toksta_app_id = get_site_option('ps_bp_toksta_app_id');
	$ps_bp_toksta_app_password = get_site_option('ps_bp_toksta_app_password');
	$ps_bp_toksta_show_siteadmin = get_site_option('ps_bp_toksta_show_siteadmin');
	$ps_bp_toksta_max_members = get_site_option('ps_bp_toksta_max_members');

	if (!$ps_bp_toksta_app_id){
		$ps_bp_toksta_app_id = '';
	}
	if (!$ps_bp_toksta_app_password){
		$ps_bp_toksta_app_password = '';
	}
?>
<div class="wrap">

<h2><?php  _e('toksta* configuration','ps_bp_toksta'); ?></h2>

<?php _e('You\'ll find the application id and password on the toksta* create script page, just below the title, eg:
<blockquote style="padding:10px;/*margin-left:40px;*/background-color:#fff;width:60%;">
	<h1 style="font-size: 18px; margin-bottom: 5px;">Embed toksta* </h1>
	<span style="font-size: 12px; font-weight: normal;">(app_id:<span style="color:red;">111111</span>, app_password:<span style="color:red;">ABABABABABABABABABAB</span>)</span>
</blockquote>','ps_bp_toksta'); ?>

<form method="post" action="">
<input type="hidden" name="action" value="update_ps_bp_toksta" />
<table class="form-table">
<tr valign="top"> 
	<th scope="row"><label for="ps_bp_toksta_app_id"><?php _e('Application ID:','ps_bp_toksta');?></label></th> 
	<td>
		<input type="text" size="45" value="<?php echo $ps_bp_toksta_app_id; ?>" style="width: 95%;" id="ps_bp_toksta_app_id" name="ps_bp_toksta_app_id"/>
		<br/>
		<?php _e('app_id from the toksta* Create script page','ps_bp_toksta');?>				
	</td> 
</tr>
<tr valign="top"> 
	<th scope="row"><label for="ps_bp_toksta_app_password"><?php _e('Application Password:','ps_bp_toksta')?></label></th> 
	<td>
		<input type="text" size="45" value="<?php echo $ps_bp_toksta_app_password; ?>" style="width: 95%;" id="ps_bp_toksta_app_password" name="ps_bp_toksta_app_password"/>
		<br/>
		<?php _e('app_password from the toksta* Create script page','ps_bp_toksta');?>				
	</td> 
</tr>
<tr valign="top"> 
	<th scope="row"><label for="ps_bp_toksta_max_members"><?php _e('Maximum members to show:','ps_bp_toksta');?></label></th> 
	<td>
		<input type="text" size="45" value="<?php echo $ps_bp_toksta_max_members; ?>" style="width: 95%;" id="ps_bp_toksta_max_members" name="ps_bp_toksta_max_members"/>
		<br/>
		<?php _e('maximum number of members to show i the chat widget','ps_bp_toksta');?>				
	</td> 
</tr>

<tr valign="top"> 
	<th scope="row"><label for="ps_bp_toksta_show_siteadmin"><?php _e('Show Site Admin:','ps_bp_toksta')?></label></th> 
	<td>
		<input type="checkbox" size="45" <?php  if ($ps_bp_toksta_show_siteadmin) { echo 'checked="checked"'; }?>" style="width: 95%;" id="ps_bp_toksta_show_siteadmin" name="ps_bp_toksta_show_siteadmin"/>
		<br/>
		<?php _e('Show Site Admin in Chat','ps_bp_toksta');?>				
	</td> 
</tr>
</table>
<p class="submit"><input type="submit" name="Submit" value="<?php _e('Update Options','ps_bp_toksta') ?>" /></p>
</form>
</div>
<?php
}

add_action('admin_menu','ps_bp_toksta_add_admin_subpanel');


// toksa_insert_js, from http://www.toksta.com/
function ps_bp_toksta_insert_js() {
	global $userdata;
	get_currentuserinfo();

	if (is_user_logged_in()){
		
		$ps_bp_toksta_app_id = get_site_option('ps_bp_toksta_app_id');
		$ps_bp_toksta_app_password = get_site_option('ps_bp_toksta_app_password');
		$ps_bp_toksta_show_siteadmin = get_site_option('ps_bp_toksta_show_siteadmin');
		
		if ($ps_bp_toksta_app_id == "" || $ps_bp_toksta_app_password == "") {
			return false;
		}
		if (!$ps_bp_toksta_show_siteadmin && is_site_admin()) {
			return false;
		}

		$tk_name = bp_fetch_user_fullname( $userdata->ID, false ); //$userdata->user_login; //<-- please set the username here
		$tk_user_id = $userdata->ID; //$userdata->ID; //<-- please set the user ID here
		$tk_gender = ""; //<-- optional (the gender of the user can be female or 'male')
		$tk_age = ""; //<-- optional (the age of the user e.g. '23')
		$tk_single = ""; //<-- optional (is the user single? 'Yes', 'no', '0' = no information)
		$tk_profile_url =  bp_core_get_user_domain( $userdata->ID ); //""; //<-- optional (url to the users profile)
		$tk_pic = bp_core_get_avatar( $userdata->ID, 1, 96, 96, true ); //<-- optional (url of the users picture)
		$tk_city = ""; //<-- optional (city where the user is located, e.g. 'Berlin')
		$tk_zip = ""; //<-- optional (users zip code, e.g. '92553')
		$tk_country = ""; //<-- optional (country where the user is located, e.g. 'Germany')

		//DO NOT EDIT THE FOLLOWING CODE!
		$tk_user_data="name:".$tk_name. ";gender:".$tk_gender. ";age:".$tk_age. ";single:".$tk_single. ";url:".$tk_profile_url. ";pic:".$tk_pic. ";city:".$tk_city. ";tk_zip:".$tk_zip. ";country:".$tk_country;

		echo "<!-- userid:" . $tk_user_id . $tk_user_data . "-->";

		//implements the js needed by the chatWatcher and to start a chat
		$tk_hash = md5($tk_user_id.$tk_user_data . $ps_bp_toksta_app_password);
		$tk_js = "<script type='text/javascript' src='http://www.toksta.com/js/w/?tk_hash=" . $tk_hash . "&amp;app_id=". $ps_bp_toksta_app_id ."&amp;app_user_id=".$tk_user_id."&amp;tk_user_data=".rawurlencode($tk_user_data)."'></script>";
		echo $tk_js;
	};
};

add_action('wp_head', 'ps_bp_toksta_insert_js');


/* Register widgets for the core component */
function ps_bp_toksta_register_widgets() {
	$ps_bp_toksta_app_id = get_site_option('ps_bp_toksta_app_id');
	$ps_bp_toksta_app_password = get_site_option('ps_bp_toksta_app_password');
	
	if ($ps_bp_toksta_app_id == "" || $ps_bp_toksta_app_password == "") {
		return false;
	}
	register_sidebar_widget( __('Chat','ps_bp_toksta'), 'ps_bp_toksta_whos_online');
}
add_action( 'plugins_loaded', 'ps_bp_toksta_register_widgets',20 );

/*** WHO'S ONLINE WIDGET *****************/

function ps_bp_toksta_whos_online($args) {
	global $current_blog,$userdata;
	get_currentuserinfo();
    extract($args);
	$ps_bp_toksta_show_siteadmin = get_site_option('ps_bp_toksta_show_siteadmin');
	$max_members = get_site_option('ps_bp_toksta_max_members');
?>
	<?php echo $before_widget; ?>
	<?php echo $before_title
		. $widget_name
		. $after_title; ?>

	<?php $users = BP_Core_User::get_online_users($max_members) ?>

	<?php if ( $users['users']) : ?>
		<div class="avatar-block">
		<?php 
		foreach ( (array) $users['users'] as $user ) :	
			if (! $ps_bp_toksta_show_siteadmin && is_site_admin()) {
				if (count($users['users']) == 1) {
					echo '<div class="widget-error">' . __('There are no users currently online.','ps_bp_toksta') . '</div>';
					break;
				} else {
					continue;
				}
			}
				
			echo '<div class="item-avatar">';		
								
			$start_chatlink =  (is_user_logged_in() && ($user->user_id != $userdata->ID)) ? '<a href="javascript:tokstaChat(' . $user->user_id. ')" alt="'. __('Chat with ','ps_bp_toksta') . bp_fetch_user_fullname( $user->user_id, false ) .'" title="' . __('Chat with ','ps_bp_toksta') . bp_fetch_user_fullname( $user->user_id, false ) .'">' : '';
			$end_chatlink = (is_user_logged_in() && ($user->user_id != $userdata->ID)) ? '</a>' : '';
				
			echo $start_chatlink . bp_core_get_avatar( $user->user_id, 1 ) . $end_chatlink;
			echo '</div>';
		endforeach; 
		?>
		</div>
	<?php else: ?>
		<div class="widget-error">
			<?php _e('There are no users currently online.','ps_bp_toksta'); ?>
		</div>
	<?php endif; ?>

	<?php echo $after_widget; ?>
	
	<div class="clear" style="margin-bottom: 25px"></div>
	
<?php
}

?>